package org.linlinjava.litemall.admin.controller;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.linlinjava.litemall.admin.beans.annotation.LogAnno;
import org.linlinjava.litemall.admin.beans.annotation.LoginAdminId;
import org.linlinjava.litemall.admin.beans.annotation.LoginAdminShopId;
import org.linlinjava.litemall.admin.beans.dto.GoodsAccessoryDto;
import org.linlinjava.litemall.admin.beans.dto.GoodsAllinone;
import org.linlinjava.litemall.admin.beans.dto.GoodsMerchandiseDto;
import org.linlinjava.litemall.admin.beans.dto.GoodsProductDto;
import org.linlinjava.litemall.admin.beans.dto.GoodsPropertyDto;
import org.linlinjava.litemall.admin.beans.dto.GoodsSpecAddDto;
import org.linlinjava.litemall.admin.beans.dto.GoodsStatusDto;
import org.linlinjava.litemall.admin.beans.dto.GoodsStoreDto;
import org.linlinjava.litemall.admin.beans.dto.PriceDto;
import org.linlinjava.litemall.admin.service.AdminGoodsService;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.core.validator.Order;
import org.linlinjava.litemall.core.validator.Sort;
import org.linlinjava.litemall.db.domain.LitemallGoods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 管理产品控制器
 *
 * @author yliyun
 * @date 2022/09/14
 */
@Api(tags = "商品API")
@RestController
@RequestMapping("/admin/goods")
@Validated
public class AdminGoodsController {

    /**
     * 管理产品服务
     */
    @Autowired
    private AdminGoodsService adminGoodsService;

    @ApiOperation(value = "生成下一个商品编号", notes = "根据数据库中最大编号递增生成唯一商品编号")
    @GetMapping("/generateGoodsSn")
    @LogAnno
    public Object generateGoodsSn(){
        return adminGoodsService.generateGoodsSn();
    }

    @ApiModelProperty("商品列表-「三级目录」")
    @GetMapping("/categoryl3")
    @LogAnno
    public Object categoryl3(){
        return adminGoodsService.categoryThreeLinkage();
    }

    @ApiOperation("商品列表-「编辑商品序号」")
    @PostMapping("/updateGoods")
    @LogAnno
    public Object updateGoods(@RequestBody LitemallGoods goods){
        return adminGoodsService.updateGoods(goods);
    }


    @ApiOperation("商品列表-「指定商品｜已上架」")
    @GetMapping("/listpPutaway")
    @LogAnno
    public Object listpPutaway( @ApiIgnore @LoginAdminShopId Integer shopId){
        return adminGoodsService.listpPutaway(shopId);
    }

    /**
     * 列表
     * 查询商品
     *
     * @param goodsSn    货物sn
     * @param name       名字
     * @param regionId   区域id
     * @param reviewType 检查类型
     * @param shopId     商店id
     * @param page       页面
     * @param limit      限制
     * @param sort       排序
     * @param order      订单
     * @return {@link Object}
     */
    @GetMapping("/list")
    @LogAnno
    public Object list(String goodsSn, String name, Integer regionId, Integer reviewType,
    				   @LoginAdminShopId Integer shopId,
                       @RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10000") Integer limit,
                       @Sort @RequestParam(defaultValue = "add_time") String sort,
                       @Order @RequestParam(defaultValue = "desc") String order,
                       @RequestParam("categoryl3") Integer categoryl3) {
        return adminGoodsService.list(goodsSn, name, shopId, regionId, reviewType, page, limit, sort, order,categoryl3);
    }


    @GetMapping("/all")
    @LogAnno
    public Object all(@LoginAdminShopId Integer shopId) {
        return adminGoodsService.allGoods(shopId);
    }

    @GetMapping("/filterGoodsByCategoryl3")
    @LogAnno
    public Object filterGoodsByCategoryl3(@LoginAdminShopId Integer shopId,Integer categoryl3) {
        return adminGoodsService.filterGoodsByCategoryl3(shopId,categoryl3);
    }


    /**
     * 店铺查询商品
     *
     * @param goodsSn    货物sn
     * @param name       名字
     * @param regionId   区域id
     * @param reviewType 检查类型
     * @param shopId     商店id
     * @param page       页面
     * @param limit      限制
     * @param sort       排序
     * @param order      订单
     * @return {@link Object}
     */
    @GetMapping("/shopList")
    @LogAnno
    public Object shopList(String goodsSn, String name, Integer regionId, Integer reviewType,
    		@LoginAdminShopId Integer shopId,
    		@LoginAdminId Integer adminId,
    		@RequestParam(defaultValue = "1") Integer page,
    		@RequestParam(defaultValue = "10") Integer limit,
    		@Sort @RequestParam(defaultValue = "add_time") String sort,
    		@Order @RequestParam(defaultValue = "desc") String order,
                           @RequestParam("categoryl3")Integer categoryl3) {
    	return adminGoodsService.shopList(goodsSn, name, shopId, adminId,regionId, reviewType, page, limit, sort, order,categoryl3);
    }

    /**
     * 用于
     *
     * @return {@link Object}
     */
    @GetMapping("/catAndBrand")
    @LogAnno
    public Object list2() {
        return adminGoodsService.list2();
    }

    /**
     * 编辑商品
     *
     * @param goodsAllinone 货物allinone
     * @param shopId        商店id
     * @return {@link Object}
     */
    @PostMapping("/update")
    @LogAnno
    public Object update(@RequestBody GoodsAllinone goodsAllinone, @LoginAdminShopId Integer shopId) {
        return adminGoodsService.update(goodsAllinone, shopId);
    }

    /**
     * 删除商品
     *
     * @param goods  货物
     * @param shopId 商店id
     * @return {@link Object}
     */
    @PostMapping("/delete")
    @LogAnno
    public Object delete(@RequestBody LitemallGoods goods, @LoginAdminShopId Integer shopId) {
        return adminGoodsService.delete(goods, shopId);
    }

    /**
     * 批量添加商品
     *
     * @param goodsAllinone 货物allinone
     * @return {@link Object}
     */
    @PostMapping("/batch")
    @LogAnno
    public Object batch(@RequestBody GoodsAllinone goodsAllinone) {
        if(goodsAllinone.getShopIds() != null && goodsAllinone.getShopIds().size() > 0){
            for(Integer shopId : goodsAllinone.getShopIds()){
                goodsAllinone.getGoods().setShopId(shopId);
                adminGoodsService.create(goodsAllinone, shopId);
            }
        }
        return ResponseUtil.ok();
    }

    /**
     * 添加商品
     *
     * @param goodsAllinone 货物allinone
     * @param shopId        商店id
     * @return {@link Object}
     */
    @PostMapping("/create")
    @LogAnno
    public Object create(@RequestBody GoodsAllinone goodsAllinone, @LoginAdminShopId Integer shopId) {
        return adminGoodsService.create(goodsAllinone, shopId);
    }

    /**
     * 商品详情
     *
     * @param id id
     * @return {@link Object}
     */
    @GetMapping("/detail")
    @LogAnno
    public Object detail(@NotNull @RequestParam(value = "id") Integer id) {
        return adminGoodsService.detail(id);
    }

    /**
     * 日志查询商品列表
     * 商品日志
     *
     * @param goodsId   商品id
     * @param goodsName 商品名称
     * @param goodsSn   货物sn
     * @param userName  用户名
     * @param content   内容
     * @param page      页面
     * @param limit     限制
     * @param sort      排序
     * @param order     订单
     * @return {@link Object}
     */
    @GetMapping("/queryGoodsLogList")
    @LogAnno
    public Object queryGoodsLogList(Integer goodsId, String goodsName, String goodsSn, String userName, String content, Integer page,
                                    Integer limit, String sort, String order){
        return adminGoodsService.queryGoodsLogList(goodsId, goodsName, goodsSn, userName, content, page, limit, sort, order);
    }

    /**
     * 商品关键字搜索
     *
     * @param keyword 关键字
     * @return {@link Object}
     */
    @GetMapping("/option")
    @LogAnno
    public Object queryByKeyword(String keyword) {
        return ResponseUtil.ok(adminGoodsService.queryByKeyword(keyword));
    }

    /**
     * 商品上架/下架
     *
     * @param goodsStatusDto 货物状态dto
     * @param shopId         商店id
     * @return {@link Object}
     */
    @PutMapping("/push")
    @LogAnno
    public Object push(@RequestBody GoodsStatusDto goodsStatusDto, @LoginAdminShopId Integer shopId) {

        return adminGoodsService.updateGoodsStatus(goodsStatusDto, shopId);

    }

    /**
     * 商品新品/非新品
     *
     * @param goodsStatusDto 货物状态dto
     * @param shopId         商店id
     * @return {@link Object}
     */
    @PutMapping("/newProduce")
    @LogAnno
    public Object newProduce(@RequestBody GoodsStatusDto goodsStatusDto, @LoginAdminShopId Integer shopId) {
        return adminGoodsService.updateGoodsStatus(goodsStatusDto, shopId);

    }

    /**
     * 商品推荐/非推荐
     *
     * @param goodsStatusDto 货物状态dto
     * @param shopId         商店id
     * @return {@link Object}
     */
    @PutMapping("/recommend")
    @LogAnno
    public Object recommend(@RequestBody GoodsStatusDto goodsStatusDto, @LoginAdminShopId Integer shopId) {
        return adminGoodsService.updateGoodsStatus(goodsStatusDto, shopId);

    }

    /**
     * 修改商品本身价格
     *
     * @param price  价格
     * @param shopId 商店id
     * @return {@link Object}
     */
    @PutMapping("/updatePrice")
    @LogAnno
    public Object updatePrice(@RequestBody PriceDto price, @LoginAdminShopId Integer shopId) {
        return adminGoodsService.updateGoodsPrice(price, shopId);

    }

    /**
     * 修改商品规格的价格
     *
     * @param price  价格
     * @param shopId 商店id
     * @return {@link Object}
     */
    @PutMapping("/updateSpecPrice")
    @LogAnno
    public Object updateSpecPrice(@RequestBody PriceDto price, @LoginAdminShopId Integer shopId) {
        return adminGoodsService.updateSpecPrice(price, shopId);

    }

    /**
     * 查询商品的价格详情
     *
     * @param goodsId 商品id
     * @param shopId  商店id
     * @return {@link Object}
     */
    @GetMapping("/allPrice")
    @LogAnno
    public Object allPrice(@NotNull @RequestParam(value = "goodsId") Integer goodsId, @LoginAdminShopId Integer shopId) {
        return adminGoodsService.allPrice(goodsId, shopId);

    }

    /**
     * 修改商品库存
     *
     * @param storeDto 商店dto
     * @param shopId   商店id
     * @return {@link Object}
     */
    @PutMapping("/updateStore")
    @LogAnno
    public Object updateStore(@RequestBody GoodsStoreDto storeDto, @LoginAdminShopId Integer shopId) {
        return adminGoodsService.updateStore(storeDto, shopId);

    }

    /**
     * 根据商品ID 查询商品会员价格
     *
     * @param goodsId 商品id
     * @return {@link Object}
     */
    @PutMapping("/queryVipGoodsPrice")
    @LogAnno
    public Object queryVipGoodsPrice(@NotNull @RequestParam(value = "goodsId") Integer goodsId){
        return adminGoodsService.queryVipGoodsPrice(goodsId);
    }

    /**
     * 根据商品ID 修改商品优惠价格（包括会员价格、满减价格、阶梯价格）
     *
     * @param goodsAllinone 货物allinone
     * @return {@link Object}
     */
    @PutMapping("/updateDiscountPrice")
    @LogAnno
    public Object updateVipGoodsPrice(@RequestBody GoodsAllinone goodsAllinone){
        return adminGoodsService.updateDiscountPrice(goodsAllinone);
    }


    /**
     * 设置商品库存和价格
     *
     * @param dto dto
     * @return {@link Object}
     */
    @PostMapping("/addGoodsProduct")
    @LogAnno
    public Object addGoodsProduct(@RequestBody GoodsProductDto dto){
        return adminGoodsService.addGoodsProduct(dto);
    }

    /**
     * 修改商品库存和价格
     *
     * @param dto dto
     * @return {@link Object}
     */
    @PutMapping("/updateGoodsProduct")
    @LogAnno
    public Object updateGoodsProduct(@RequestBody GoodsProductDto dto){
        return adminGoodsService.updateGoodsProduct(dto);
    }

    /**
     * 查看商品库存和价格
     *
     * @param shopId  商店id
     * @param goodsId 商品id
     * @return {@link Object}
     */
    @GetMapping("/readGoodsProduct")
    @LogAnno
    public Object readGoodsProduct(@LoginAdminShopId Integer shopId, @NotNull Integer goodsId){
        return adminGoodsService.readGoodsProduct(goodsId, shopId);
    }

    /**
     * 产品运维
     *
     * @param shopId 商店id
     * @return {@link Object}
     */
    @GetMapping("/goodsOps")
    @LogAnno
    public Object goodsOps(@LoginAdminShopId Integer shopId){
        return adminGoodsService.all(shopId);
    }

    /**
     * 新增规格原料
     *
     * @param goodsMerchandiseDto 货物商品dto
     * @return {@link Object}
     */
    @PostMapping("/addSpecMer")
    @LogAnno
    public Object addSpecMer(@Valid @RequestBody GoodsMerchandiseDto goodsMerchandiseDto){
    	return adminGoodsService.addSpecMer(goodsMerchandiseDto);
    }

    /**
     * 修改规格原料
     *
     * @param goodsMerchandiseDto 货物商品dto
     * @return {@link Object}
     */
    @PostMapping("/updateSpecMer")
    @LogAnno
    public Object updateSpecMer(@Valid @RequestBody GoodsMerchandiseDto goodsMerchandiseDto){
    	return adminGoodsService.updateSpecMer(goodsMerchandiseDto);
    }

    /**
     * 删除规格原料
     *
     * @param specMerId 规范mer id
     * @return {@link Object}
     */
    @GetMapping("/deleteSpecMer")
    @LogAnno
    public Object deleteSpecMer(@NotNull Integer specMerId){
    	return adminGoodsService.deleteSpecMer(specMerId);
    }

    /**
     * 新增规格
     *
     * @param goodsSpecAddDto 商品规格添加dto
     * @return {@link Object}
     */
    @PostMapping("/addSpec")
    @LogAnno
    public Object addSpec(@Valid @RequestBody GoodsSpecAddDto goodsSpecAddDto){
        return adminGoodsService.addSpec(goodsSpecAddDto);
    }

    /**
     * 修改规格
     *
     * @param goodsSpecAddDto 商品规格添加dto
     * @return {@link Object}
     */
    @PostMapping("/updateSpec")
    @LogAnno
    public Object updateSpec(@Valid @RequestBody GoodsSpecAddDto goodsSpecAddDto){
    	return adminGoodsService.updateSpec(goodsSpecAddDto);
    }

    /**
     * 删除规格
     *
     * @param specId 规格id
     * @return {@link Object}
     */
    @GetMapping("/deleteSpec")
    @LogAnno
    public Object deleteSpec(@NotNull Integer specId){
    	return adminGoodsService.deleteSpec(specId);
    }

    /**
     * 查询规格的原料列表
     *
     * @param merchandiseId 原料id(可继续选择的)
     * @param specId        规格id
     * @return {@link Object}
     */
    @GetMapping("/specMerList")
    @LogAnno
    public Object specMerList(Integer merchandiseId, @NotNull Integer specId){
        return adminGoodsService.specMerList(merchandiseId, specId);
    }

    /**
     * 查询辅料的原料列表
     *
     * @param name          名字
     * @param merchandiseSn 商品sn
     * @return {@link Object}
     */
    @GetMapping("/accessoryMerList")
    @LogAnno
    public Object accessoryMerList(String name, String merchandiseSn){
    	return adminGoodsService.accessoryMerList(name, merchandiseSn);
    }

    /**
     * 保存辅料
     *
     * @param goodsAccessoryDto 原料dto
     * @return {@link Object}
     */
    @PostMapping("/addAccessory")
    @LogAnno
    public Object addAccessory(@Valid @RequestBody GoodsAccessoryDto goodsAccessoryDto){
    	return adminGoodsService.addAccessory(goodsAccessoryDto);
    }

    /**
     * 更新辅料
     *
     * @param goodsAccessoryDto 原料dto
     * @return {@link Object}
     */
    @PostMapping("/updateAccessory")
    @LogAnno
    public Object updateAccessory(@Valid @RequestBody GoodsAccessoryDto goodsAccessoryDto){
    	return adminGoodsService.updateAccessory(goodsAccessoryDto);
    }

    /**
     * 删除辅料
     *
     * @param accessoryId 辅料id
     * @return {@link Object}
     */
    @GetMapping("/deleteAccessory")
    @LogAnno
    public Object deleteAccessory(@NotNull Integer accessoryId){
    	return adminGoodsService.deleteAccessory(accessoryId);
    }

    /**
     * 复制商品
     *
     * @param id     id
     * @param shopId 商店id
     * @return {@link Object}
     */
    @GetMapping("/copy")
    @LogAnno
    public Object copy(@NotNull Integer id, @LoginAdminShopId Integer shopId) {
        return adminGoodsService.copy(id,shopId);
    }

    /**
     * 保存属性
     *
     * @param goodsPropertyDto 属性dto
     * @return {@link Object}
     */
    @PostMapping("/addProp")
    @LogAnno
    public Object addProp(@Valid @RequestBody GoodsPropertyDto goodsPropertyDto){
    	return adminGoodsService.addProperty(goodsPropertyDto);
    }

    /**
     * 修改属性
     *
     * @param goodsPropertyDto 属性dto
     * @return {@link Object}
     */
    @PostMapping("/updateProp")
    @LogAnno
    public Object updateProp(@Valid @RequestBody GoodsPropertyDto goodsPropertyDto){
    	return adminGoodsService.updateProperty(goodsPropertyDto);
    }

    /**
     * 删除属性
     *
     * @param propertyId 属性id
     * @return {@link Object}
     */
    @GetMapping("/deleteProp")
    @LogAnno
    public Object deleteProp(@NotNull Integer propertyId){
    	return adminGoodsService.deleteProperty(propertyId);
    }


}
